package com.googlecode.animator.view.properties
{
	import com.googlecode.animator.data.Notifications;
	import com.googlecode.animator.data.objects3d.CameraProxyVO;
	import com.googlecode.animator.data.objects3d.CubeVO;
	import com.googlecode.animator.data.objects3d.Object3DVO;
	import com.googlecode.animator.data.objects3d.PlaneVO;
	import com.googlecode.animator.data.objects3d.SphereVO;
	import com.googlecode.animator.data.objects3d.TargetProxyVO;
	import com.googlecode.animator.events.PropertiesPanelEvent;
	import com.googlecode.animator.model.ProjectSettingsProxy;
	import com.googlecode.animator.view.properties.types.Depth;
	import com.googlecode.animator.view.properties.types.Height;
	import com.googlecode.animator.view.properties.types.Object3DName;
	import com.googlecode.animator.view.properties.types.Width;
	import com.googlecode.animator.view.properties.types.Xpos;
	import com.googlecode.animator.view.properties.types.Ypos;
	import com.googlecode.animator.view.properties.types.Zpos;
	
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class PropertiesPanelMediator extends Mediator
	{
		/**
		 * Identifier. 
		 */
		public static const NAME:String = "PropertiesPanelMediator";
		
		private var _object3D:Object3DVO;
		
		public function PropertiesPanelMediator(viewComponent:PropertiesPanel)
		{
			super(NAME, viewComponent);

		}
		
		override public function onRegister():void
		{
			super.onRegister();
			
			viewRef.addEventListener(MouseEvent.CLICK, onClick);
			viewRef.addEventListener(PropertiesPanelEvent.NAME, onPropertyChange);
			viewRef.editMaterialsBtn.visible = false;
		}
		
		override public function onRemove():void
		{
			super.onRemove();
			
			viewRef.removeEventListener(MouseEvent.CLICK, onClick);
			viewRef.propertiesPanel.removeEventListener(PropertiesPanelEvent.NAME, onPropertyChange);
		}
		
		override public function listNotificationInterests():Array
		{
			var list:Array = super.listNotificationInterests();
			
			list.push(Notifications.OBJECT3D_SELECTED);
			list.push(Notifications.OBJECT3D_POSITION_CHANGED);
			list.push(Notifications.OBJECT3D_DELETED);
			
			return list;
		}
		
		override public function handleNotification(notification:INotification):void
		{
			super.handleNotification(notification);
			
			switch(notification.getName())
			{
				case Notifications.OBJECT3D_SELECTED:
					
					handleObject3DSelected(notification);
					
					break;
				case Notifications.OBJECT3D_POSITION_CHANGED:
					
					handleObject3DPositionChanged(notification);
					
					break;
					
				case Notifications.OBJECT3D_DELETED:
					
					handleObject3DDeleted();
	
					break;
					
				
			}
		}
		
		protected function handleObject3DSelected(notification:INotification):void
		{
			viewRef.cleanup();
			
			
			var name:String = notification.getBody() as String;
			_object3D = settings.getObject3D(name);

			if(_object3D is CameraProxyVO || _object3D is TargetProxyVO)
			{
				viewRef.editMaterialsBtn.visible = false;
			}else
			{
				viewRef.editMaterialsBtn.visible = true;
			}
			
			coreProperties(_object3D);
			
			if(_object3D is PlaneVO)
			{
				handlePlaneSelected(_object3D as PlaneVO);
			}
			
			if(_object3D is CubeVO)
			{
				handleCubeSelected(_object3D as CubeVO);
			}
			
			if(_object3D is SphereVO)
			{
				handleSphereSelected(_object3D as SphereVO);
			}

		}
		
		protected function handleObject3DPositionChanged(notification:INotification):void
		{
			var name:String = notification.getBody() as String;
			var VO:Object3DVO = settings.getObject3D(name);
			
			viewRef.update(Xpos.NAME, VO.x);
			viewRef.update(Ypos.NAME, VO.y);
			viewRef.update(Zpos.NAME, VO.z);
		}
		
		protected function handleObject3DDeleted():void
		{
			viewRef.cleanup();
			viewRef.editMaterialsBtn.visible = false;
			//viewRef.closePopup();
		}
		
		protected function coreProperties(VO:Object3DVO):void
		{
			var name:Object3DName = new Object3DName();
			name.label = "Name:";
			name.propertyValue = VO.label;
			
			var xPos:Xpos = new Xpos();
			xPos.label = "X Position:";
			xPos.propertyValue = String(VO.x);
			
			var yPos:Ypos = new Ypos();
			yPos.label = "Y Position:";
			yPos.propertyValue = String(VO.y);
			
			var zPos:Zpos = new Zpos();
			zPos.label = "Z Position:";
			zPos.propertyValue = String(VO.z);
			
			viewRef.addProperty(name);
			viewRef.addProperty(xPos);
			viewRef.addProperty(yPos);
			viewRef.addProperty(zPos);
		}
		
		protected function handlePlaneSelected(VO:PlaneVO):void
		{
			
			var w:Width = new Width();
			var h:Height = new Height();
			
			w.label = "Width:";
			w.propertyValue = String(int(VO.width * VO.scaleX));
			
			h.label = "Height:";
			h.propertyValue = String(int(VO.height * VO.scaleY));
			
			viewRef.addProperty(w);
			viewRef.addProperty(h);
		}
		
		protected function handleCubeSelected(VO:CubeVO):void
		{
			var w:Width = new Width();
			var h:Height = new Height();
			var d:Depth = new Depth();
			
			w.label = "Width:";
			w.propertyValue = String(int(VO.width * VO.scaleX));
			
			h.label = "Height:";
			h.propertyValue = String(int(VO.height * VO.scaleY));
			
			d.label = "Depth:";
			d.propertyValue = String(int(VO.depth * VO.scaleZ));
			
			viewRef.addProperty(w);
			viewRef.addProperty(h);
			viewRef.addProperty(d);
		}
		
		protected function handleSphereSelected(VO:SphereVO):void
		{
			
		}
		
		protected function onPropertyChange(evt:PropertiesPanelEvent):void
		{
			(evt as PropertiesPanelEvent).VO.name = _object3D.name;
			
			sendNotification(Notifications.UPDATE_OBJECT3D_PROPERTY, evt);
		}
		
		protected function onClick(evt:MouseEvent):void
		{
			if(evt.target is MaterialsPopupButton)
			{
				viewRef.openPopup();
				//var materials
				facade.registerMediator(new PropertiesPopupMediator(viewRef.popup as PropertiesPopup));
			}
		}
		
		protected function get settings():ProjectSettingsProxy
		{		
			return facade.retrieveProxy(ProjectSettingsProxy.NAME) as ProjectSettingsProxy;
		}
		
		/**
		 * Returns typed reference to component. 
		 * @return 
		 * 
		 */
		protected function get viewRef():PropertiesPanel
		{
			return viewComponent as PropertiesPanel
		}
	}
}